package cn.itcast.controller;

import cn.itcast.pojo.Dept;
import cn.itcast.pojo.Result;
import cn.itcast.service.DeptService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import java.util.List;

@RestController
@RequestMapping("/depts")
@CrossOrigin("*")
@Slf4j
public class DeptController {
    @Autowired
    private DeptService deptService;
    @GetMapping
    public Result findAll() {
        List<Dept> list = deptService.findAll();
        return list == null || list.size() == 0 ? Result.error("success") : Result.success("查询成功", list);
    }
    @DeleteMapping
    public Result deleteById(Integer id) {
        boolean result = deptService.deleteById(id);
        return result ? Result.success("删除成功") : Result.error("删除失败");
    }
    @PostMapping
    public Result add(@RequestBody Dept dept) {
        boolean result = deptService.add(dept);
        return result ? Result.success("添加成功") : Result.error("添加失败");
    }
    @GetMapping("/{id}")
    public Result findById(@PathVariable Integer id) {
        Dept dept = deptService.findById(id);
        return dept == null ? Result.error("查询失败") : Result.success("查询成功", dept);
    }

    @PutMapping
    public Result update(@RequestBody Dept dept){
        boolean result = deptService.update(dept);
        return result ? Result.success("修改成功") : Result.error("修改失败");
    }

}
